Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add PoissonNLL loss #2765

Merged
merged 5 commits into from
Feb 3, 2025
Merged

Feat: Add PoissonNLL loss #2765

merged 5 commits into from
Feb 3, 2025

Conversation

salvomcl
Copy link
Contributor

@salvomcl salvomcl commented Feb 2, 2025

Pull Request Template

Checklist

  • Confirmed that run-checks all script has been executed.
  • Made sure the book is up to date with changes in this PR.

Related Issues/PRs

This PR addresses issue #2751.

Changes

This PR introduces the PoissonNLLLoss module, implementing the Poisson negative log-likelihood loss function.
In particular I followed thepytorch implementation to make the transition from PyTorch as smooth as possible for users.

Key additions:

  • PoissonNLLLossConfig: Configuration structure with parameters for log_input, full, and eps.
  • PoissonNLLLoss: Implements forward and element-wise loss computation with optional reduction (Mean, Sum).
  • Validation checks to ensure appropriate input dimensions and non-negative target values.

Testing

  • Added unit tests to verify:
    • Correctness of the loss computation with different configurations.
    • Behavior when log_input is true and false.
    • Handling of the Stirling approximation term when full is enabled.
    • Gradient computation correctness via backpropagation tests.
    • Assertion failures for invalid inputs (negative targets, shape mismatches, and non-positive eps).

@salvomcl salvomcl marked this pull request as ready for review February 2, 2025 17:04
Copy link
Member

@laggui laggui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for the addition 🙂

Just some minor changes left

/edit: also, ignore the audit failure on the CI - this is fixed on main.

crates/burn-core/src/nn/loss/poisson.rs Outdated Show resolved Hide resolved
crates/burn-core/src/nn/loss/poisson.rs Outdated Show resolved Hide resolved
loss_sum.into_data().assert_approx_eq(&expected, 5);
}

#[test]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TestAutodiffBackend type is under the std feature flag, so we probably need to guard this test.

Suggested change
#[test]
#[cfg(feature = "std")]
#[test]

Copy link

codecov bot commented Feb 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.62%. Comparing base (cb0854c) to head (852a33a).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2765      +/-   ##
==========================================
+ Coverage   83.57%   83.62%   +0.05%     
==========================================
  Files         824      825       +1     
  Lines      108453   108686     +233     
==========================================
+ Hits        90639    90893     +254     
+ Misses      17814    17793      -21     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@salvomcl
Copy link
Contributor Author

salvomcl commented Feb 3, 2025

Thanks for the review @laggui! Just pushed the requested changes! :)

@laggui laggui merged commit 9f00320 into tracel-ai:main Feb 3, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants